package weaver.interfaces.zjj.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringEscapeUtils;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zjj.utils.ZjjUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-06-23
 * @Description: ${description}
 * @ModifyLog:
 **/

public class WeaPushWantAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String remark = requestInfo.getRequestManager().getRemark();
        String curdate = DateUtil.getCurrentDate();
        JSONObject resultJson = new JSONObject(  );
        RecordSet rs = new RecordSet();
        writeLog( ">>>>>>WeaPushWantAction>>>>>requestid="+requestid );
        try {
            rs.executeQuery( "select rwdwybs,gzxwybs,fkfj,dxdfnr from " + tablename + " where requestid=?", requestid );
            rs.next();
            JSONObject params = new JSONObject();
            params.put( "IdentityGuid", "Epoint_WebSerivce_**##0601" );
            params.put( "TaskGuid", Util.null2String( rs.getString( "rwdwybs" ) ));
            params.put( "MessageItemGuid", Util.null2String( rs.getString( "gzxwybs" ) ));
            remark = StringEscapeUtils.unescapeHtml(ZjjUtil.stripHtml(remark));
            params.put( "FeedBackContent",remark);
            params.put( "PromiseDate", DateUtil.addDate(curdate,2 ));
            params.put( "UserGuid", "186566dd-5baf-4789-b5ea-52328a029ac3");
            params.put( "PithyContent", Util.null2String( rs.getString( "dxdfnr" ) ));
            String fkfj = Util.null2String( rs.getString( "fkfj" ) );
            JSONArray attach = new JSONArray();
            if(!"".equals( fkfj )){
                rs.executeQuery("select imagefilename,imagefileid ,iszip from imagefile where" +
                        " imagefileid in (select imagefileid from docimagefile where docid in ("+fkfj+"))");
                while (rs.next()){
                    JSONObject attachJson = new JSONObject();
                    String imagefilename = Util.null2String( rs.getString( "imagefilename" ) );
                    int imagefileid = Util.getIntValue( rs.getString( "imagefileid" ) );
                    InputStream inputStream = ZjjUtil.getInputStreamById(imagefileid);
                    ByteArrayOutputStream result = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    for (int length; (length = inputStream.read(buffer)) != -1; ) {
                        result.write(buffer, 0, length);
                    }
                    byte[] bytes = result.toByteArray();
                    JSONObject resultJson_file = ZjjUtil.UploadThirdFile(imagefilename,bytes);
                    attachJson.put( "FileNumber", resultJson_file.getString("FileNumber"));
                    attachJson.put( "FileName", imagefilename);
                    attachJson.put( "UploadDate", curdate );
                    attach.add( attachJson );
                }
                params.put( "attach", attach );

                resultJson = ZjjUtil.FeedBack_Opinion2( params );
            }else {
                resultJson = ZjjUtil.FeedBack_Opinion( params );
            }
            writeLog( "params="+params.toJSONString() );
            writeLog( "resultJson="+resultJson.toJSONString() );
            String Status = Util.null2String(resultJson.getString("Status"));
            String Description = Util.null2String(resultJson.getString("Description"));
            Description = new String(Description.getBytes(), "UTF-8");
            if("0".equals( Status )){
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "12345系统接口返回信息："+Description );
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
